package main;

import java.util.Vector;

import data.Adjectif;
import data.Item;
import data.Prefix;
import data.Suffix;
import data.Type;

public class Randomizer {

	public static int[] distribPref = { 75, 25 };
	public static int[] distribAdj = { 15, 50, 34, 1 };
	public static int[] distribSuf = { 5, 80, 14, 1 };
	public static int[] distribSufAdj = { 75, 25 };
	public Vector<Type> types = new Vector<Type>();
	public Vector<Prefix> prefixes = new Vector<Prefix>();
	public Vector<Adjectif> adjectives = new Vector<Adjectif>();
	public Vector<Adjectif> adjectivesWeapon = new Vector<Adjectif>();
	public Vector<Adjectif> adjectivesSuffix = new Vector<Adjectif>();
	public Vector<Suffix> suffixes = new Vector<Suffix>();

	public static void main(String[] args) {
		Randomizer r = new Randomizer();
		r.init();
		for (int i = 0; i < 10; i++) {
			System.out.println(r.getRandomItem().toString().trim());
			System.out.print("\n");
		}
	}

	private Item getRandomItem() {

		// how many Adjectives? 0-3
		int nbAdj = getRandomNumber(distribAdj);
		// How many suffixes? 0-3
		int nbSuff = getRandomNumber(distribSuf);
		// build attribute list
		Vector<Prefix> pre = new Vector<Prefix>();
		Vector<Adjectif> adj = new Vector<Adjectif>();
		Vector<Suffix> suf = new Vector<Suffix>();

		// Get one random type
		Type t = getRandomType();
		// Get one random prefix
		if (getRandomNumber(distribPref) == 1) {
			pre.add(prefixes.get((int) Math.floor(Math.random() * prefixes.size())));
		}
		// Get the random adjectives
		for (int i = 0; i < nbAdj; i++) {
			Adjectif a = adjectivesWeapon.get((int) Math.floor(Math.random()
						* adjectivesWeapon.size()));
			// No double adjective
			if (!adj.contains(a)) {
				adj.add(a);
			}
		}
		// Get the random suffixes
		for (int i = 0; i < nbSuff; i++) {
			Suffix s = suffixes.get((int) Math.floor(Math.random()
					* suffixes.size()));
			// get maybe one adjective for this suffix
			if (getRandomNumber(distribSufAdj) == 1) {
				// Get one random adjective
				s.setAdjectif(adjectivesSuffix.get((int) Math.floor(Math.random()
						* adjectivesSuffix.size())));
			}
			// no double suffix
			if (!suf.contains(s)) {
				suf.add(s);
			}
		}

		return new Item(t, pre, adj, suf);
	}

	private Type getRandomType() {
		return types.get((int) Math.floor(Math.random() * types.size()));
	}

	private void init() {

		types = IOUtil.buildTypes();
		if(types == null || types.size() == 0) {
			return;
		}

		prefixes = IOUtil.buildPrefixes();
		if(prefixes == null || prefixes.size() == 0) {
			return;
		}
		
		adjectives = IOUtil.buildAdjectives();
		if(adjectives == null || adjectives.size() == 0) {
			return;
		}
		
		adjectivesWeapon = new Vector<Adjectif>(adjectives);
		adjectivesSuffix = new Vector<Adjectif>(adjectives);
		
		adjectivesWeapon.addAll(IOUtil.buildWeaponAdjectives());
		if(adjectivesWeapon == null || adjectivesWeapon.size() == 0) {
			return;
		}
		
		adjectivesSuffix.addAll(IOUtil.buildSuffixAdjectives());
		if(adjectivesSuffix == null || adjectivesSuffix.size() == 0) {
			return;
		}
		
		suffixes = IOUtil.buildSuffixes();
		if(suffixes == null || suffixes.size() == 0) {
			return;
		}
	}

	private int getRandomNumber(int[] distrib) {
		int cumul = 0;
		double r = Math.random() * 100;
		for (int i = 0; i < distrib.length; i++) {
			cumul += distrib[i];
			if (r <= cumul) {
				return i;
			}
		}
		return distrib.length;
	}

}
